<?php
namespace App\Http\Controllers\Admin;
use App\http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class BannerController extends Controller{
    public function index(){
        return view('admin/index');
        //视图文件是 resources/views/admin/index.blade.php
    }
    public function toget_banner_count(Request $request){
        $count = DB::table('banner');
        if($request->start){
            $count = $count->where('banner_addtime','>',strtotime($request->start));
        }
        if($request->end){
            $count = $count->where('banner_addtime','<',strtotime($request->end));
        }
        $count = $count->count();
        echo $count;die;

    }
    public function toget_banner_list(){
        $banner_p= DB::table('banner')
            ->orderBy("banner_addtime","DESC")
            ->paginate(5);
        //dd($banner_p);

        $banner = $banner_p->items();
        $current = $banner_p->currentPage();
        $a = ($current-1)*5+1;
        echo '
        <table class="layui-table">
            <thead>
                <tr>
                    <th>
                    <input type="checkbox" name="all" value="" />
                    </th>
                    <th>编号</th>
                    <th>广告图地址</th>
                    <th>广告连接地址</th>
                    <th>广告添加时间</th>
                    <th >操作</th>
                </tr>
            </thead>
            <tbody>
           ';
//        echo '<pre>';
//        print_r($banner);
//        echo '</pre>';
        foreach ($banner as $value){
            $banner_time = date('Y-m-d',$value->banner_addtime);
            //echo $banner_time;
            echo '
            <tr>
                <td>
                <input type="checkbox" name="id[]" value="'.$value->banner_id.'" />
                </td>
                <td>'.$a++.'</td>
                <td>'.$value->banner_path.'</td>
                <td>'.$value->banner_url.'</td>
                <td>'.$banner_time.'</td>
                <td class="td-manage">
                    <a title="查看"  onclick="x_admin_show('."'编辑'".','."'/admin/banner_edit/$value->banner_id'".')" href="javascript:;">
                        <i class="layui-icon">&#xe63c;</i>
                    </a>
                    <a title="删除" onclick="member_del(this,'.$value->banner_id.')" href="javascript:;">
                        <i class="layui-icon">&#xe640;</i>
                    </a>
                </td>
            </tr>
            ';
        }
        echo '
            </tbody>
            </table>
            <div class="page">'.$banner_p->render().'
            </div>
        ';
    }
    public function banner_list(){

        // echo 13213;
//        show_msg('3213','/');
        return view('admin/banner/banner_list');
    }
    public function banner_add(){


        return view('admin/banner/banner_add');
    }
    public function todo_addbanner(Request $request){
        $data = array(
            "banner_url"=>$request->banner_url,
            "banner_addtime"=>strtotime($request->banner_addtime),
        );
        if($request->hasFile('file')&&$request->file('file')->isValid()){
            $file=$request->file('file');
            $allowed_extensions = ["png", "jpg", "gif"];
            if (!in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                echo json_encode(array(
                    "code"=>"2",
                    "msg"=>"只能上传png,jpg和gif格式的图片"
                ));die;
            }else{
                $destinationPath = 'uploads/banner/'; //public 文件夹下面建 uploads 文件夹
                $extension = $file->getClientOriginalExtension();
                $fileName=md5(time().rand(1,1000)).'.'.$extension;
                $file->move($destinationPath,$fileName);
                $data["banner_path"]="banner/".$fileName;

            }
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"图片上传失败请重试"
            ));die;
        }
        $res = DB::table("banner")->insert($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"添加成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }

    }
    public function banner_edit($banner_id){
        $banner = DB::table('banner')->where('banner_id',$banner_id)->first();
        $banner->banner_addtime = date('Y-m-d',$banner->banner_addtime);
        return view('admin/banner/banner_edit',['banner'=>$banner]);
    }
    public function togo_banner_edit(Request $request){

        $data = array(
            "banner_url"=>$request->banner_url,
            "banner_addtime"=>strtotime($request->banner_addtime)
        );
        if($request->hasFile('file')&&$request->file('file')->isValid()){
            $file=$request->file('file');
            $allowed_extensions = ["png", "jpg", "gif"];
            if (!in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                echo json_encode(array(
                    "code"=>"2",
                    "msg"=>"只能上传png,jpg和gif格式的图片"
                ));die;
            }else{
                //删除旧图片
                $old=DB::table('banner')->select('banner_path')->where("banner_id",$request->banner_id)->first();
                $banner_path=$old->banner_path;//文件名
                $path='./uploads/'.$banner_path;//完整路径
                if(!empty($banner_path) && file_exists($path)){
                    unlink($path);//当图片字段不为空并且有该图片文件的时候就删除
                }
                $destinationPath = 'uploads/banner/'; //public 文件夹下面建 uploads 文件夹
                $extension = $file->getClientOriginalExtension();
                $fileName=md5(time().rand(1,1000)).'.'.$extension;
                $file->move($destinationPath,$fileName);
                $data["banner_path"]="banner/".$fileName;

            }
        }
        $res = DB::table("banner")->where('banner_id',$request->banner_id)->update($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"编辑成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    public function todo_banner_del(Request $request){
        $banner_id = $request->banner_id;
        $res = DB::table("banner")->where("banner_id",$banner_id)->delete();
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    public function todo_banner_delall(Request $request){

        $res = DB::table('banner')->whereIn("banner_id",$request->arr)->delete();
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
}